﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;

namespace School.Admission
{
    public class Father
    {
        private static SchoolEntities db = new SchoolEntities();

        public static Boolean Add(SD_Father  father)
        {
            try
            {
                db.AddToSD_Father(father);
                db.SaveChanges();

                return true;
            }

            catch
            {
                return false;

            }
        }



        public static SD_Father Single(long id)
        {
            return db.SD_Father.Single(x => x.Id == id);
        }



        public static SD_Father SingleByStudentId(long sid)
        {
            try
            {
                return db.SD_Father.Single(x => x.Student_Id == sid);
            }
            catch
            {
                return null;
            }
        }



        public static IEnumerable<SD_Father> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from father  in db.SD_Father select father;
           
            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Ar_FirstName")
                {
                    query = query.Where(c => c.Ar_FirstName == item.Value);
                }

                if (item.Key == "Ar_SecondName")
                {
                    query = query.Where(c => c.Ar_SecondName == item.Value);
                }

                if (item.Key == "Ar_ThirdName")
                {
                    query = query.Where(c => c.Ar_ThirdName  == item.Value);
                }


                if (item.Key == "Ar_FourthName")
                {
                    query = query.Where(c => c.Ar_FourthName == item.Value);
                }


            }
            return query.ToList();
        }

        public static void Update(SD_Father  father)
        {
            SD_Father c = db.SD_Father.Single(x => x.Id == father.Id);
          
            
            c.Id = father.Id;
            c.Student_Id = father.Student_Id;

            c.Ar_FirstName = father.Ar_FirstName;
            c.Ar_SecondName = father.Ar_SecondName;
            c.Ar_ThirdName = father.Ar_ThirdName;
            c.Ar_FourthName = father.Ar_FourthName;

            c.En_FirstName = father.En_FirstName;
            c.En_SecondName = father.En_SecondName;
            c.En_ThirdName = father.En_ThirdName;
            c.En_FourthName = father.En_FourthName;

            c.Gregorian_BirthDate = father.Gregorian_BirthDate;
            c.Hijri_BirthDate = father.Hijri_BirthDate;
            c.Identification_Number = father.Identification_Number;
            c.Pasport_Number = father.Pasport_Number;
            c.Qualification_Id = father.Qualification_Id;
            c.Specialization = father.Specialization;
            c.PointOfGraduation = father.PointOfGraduation;
            c.Greggorian_GraduationDate = father.Greggorian_GraduationDate;
            c.Hijri_GraduationDate = father.Hijri_GraduationDate;
            c.HomeAddress_Id = father.HomeAddress_Id;
            c.WorkAddress_Id = father.WorkAddress_Id;
            c.Nationality_Id = father.Nationality_Id;

            c.UserName = father.UserName;
            c.Password = father.Password;
            
            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.SD_Father.DeleteObject(db.SD_Father.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static SD_Father GetByUserNamePassword(String UserName, String pass)
        {
            var data = from x in db.SD_Father
                       where x.UserName == UserName
                       && x.Password == pass
                       select x;

            if (data.Count() > 0)
                return data.First();
            else
                return null;
        }

        public static List<TE_Student> GetFatherstudents(int fatherId)
        {
            string qry = string.Format(@"select * from TE_Student
where TE_Student.Id in (
select SD_Father.Student_Id from SD_Father where SD_Father.Identification_Number =
(select SD_Father.Identification_Number from SD_Father where SD_Father.Id = {0} ))
", fatherId);
            return db.ExecuteStoreQuery<TE_Student>(qry).ToList();
        }
        public static bool CheckUserNameFound(string userName, long fatherId)
        {
            return (db.SD_Father.SingleOrDefault(i => i.UserName.ToLower() == userName.ToLower() && i.Id != fatherId) != null);
        }

    }
}
